using UnityEngine;
using System.Collections;
using System.Reflection;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System;
using System.Threading;
using System.Text;
using System.IO;
using UnityEngine.UI;


#region 定义全局的委托   Global delegate 委托

/// <summary>
/// UI对象状态改变的事件，委托类型 : 对象，新的的状态，老的状态。
/// </summary>
/// <param name="result">Result.</param>
public delegate void StateChangedEvent(object sender, EnumObjectState newState, EnumObjectState oldState);

// 定义一个消息机制的全局委托
public delegate void MessageEvent(Message message);

#endregion

#region ui框架的全局枚举对象     Global enum 枚举
/// <summary>
/// 对象当前状态 
/// </summary>
public enum EnumObjectState
{
    /// <summary>
    /// 未知状态
    /// </summary>
    None,
    /// <summary>
    /// 初始化状态
    /// </summary>
    Initial,
    /// <summary>
    /// 加载的状态
    /// </summary>
    Loading,
    /// <summary>
    /// 已准备好的状态
    /// </summary>
    Ready,
    /// <summary>
    /// 隐藏的状态
    /// </summary>
    Disabled,
    /// <summary>
    /// 关闭的状态
    /// </summary>
    Closing
}

/// <summary>
/// Enum user interface type.
/// UI面板类型
/// </summary>
public enum EnumUIType : int
{
    /// <summary>
    /// The none.
    /// </summary>
    None = -1,

    MessageBoxWnd,
    TestPanel_1,
    TestUITwo,
}

#endregion


#region Defines static class & cosnt

/// <summary>
/// 路径定义。
/// </summary>
public static class UIPathDefines
{
    
    /// <summary>
    /// 通过 UI类型（EnumUIType）获取预设的路径
    /// </summary>
    /// <returns>The prefab path by type.</returns>
    public static string GetPrefabPathByType(EnumUIType _uiType)
    {
        string _path = string.Empty;   // 空字符串 
        switch (_uiType)
        {
           
            case EnumUIType.MessageBoxWnd:
                _path = "MessageBoxWnd";
                break;
            case EnumUIType.TestPanel_1:
                _path = "TestPanel_1";
                break;
            case EnumUIType.TestUITwo:
                _path = "TestUITwo";
                break;
        }

        return _path;//返回存储路径
    }

    /// <summary>
    /// 取得该UI类型对应的脚本的Type
    /// </summary>
    /// <returns>The user interface script by type.</returns>
    public static System.Type GetUIScriptByType(EnumUIType _uiType)
    {
        System.Type _scriptType = null;
        switch (_uiType)
        {
            case EnumUIType.MessageBoxWnd:
                _scriptType = typeof(MessageBoxWnd);
                break;
            case EnumUIType.TestPanel_1:
                _scriptType = typeof(TestBaseUIOneTest);
                break;
            case EnumUIType.TestUITwo:
                _scriptType = typeof(TestTwoBaseUI);
                break;
            default:
                Debug.Log("Not Find EnumUIType! type: " + _uiType.ToString());
                break;
        }
        return _scriptType;
    }
}


// YooAsset资源ip环境变量
public enum YooAssetEnvironmentType
{
    Loacal,
    DevEnv,
    ProductionEnv
}
// yooasset那个编译模式
public enum YAEnterMode
{
    EditorSimulateMode,
    WebPlayMode
}
#endregion

/// <summary>
/// 定义全局数据
/// </summary>
public class Defines
{
    static Defines()
    {
        InitializeUrls();
    }

    #region ------------------静态数据 地址------------------
    public static readonly string BaseEnemyBattleAttributeJsonPath = "BaseEnemyBattleAttributes.json";
    public static readonly string BaseEmployeBattleAttributeJsonPath = "BaseEmployeBattleAttributes.json";
    public static readonly string UserEmployeJsonPath = "UserEmployer.json";
    public static readonly string BasePveSkillJsonPath = "BasePveSkills.json";
    #endregion

    #region ------------------bundle-->Scene 地址----------------
    public static readonly string LoginSceneName = "";
    public static readonly string MainSceneName = "";
    public static readonly string TestUIScene = "TestUIScene";
    public static readonly string TestJsonScene = "TestJsonScene";

    #endregion

    #region ------------------bundle-->prefabe 地址------------------

    #endregion

    #region ------------------bundle-->ui 地址------------------
    // TODO：需要讨论下，ui或者sprite这种跟着界面或者功能走的资源名称，还需要统一放在defines里吗？还是放在该逻辑实现类里就可以呢？

    #endregion

    #region ------------------YooAsset 设置----------------
    public static readonly string localIp = "http://127.0.01:8089";
    public static readonly string BundlePackageName = "MainPackage";
    public static readonly string BundleVer = "ptv0.0.9";
    #endregion

    #region ------------------各种Ip 地址--------------------------
    // bundle下载地址
    public static string BundleServerURL;

    public static string ServerURL;

    #endregion----------------------------------------------------

    #region -------------------ui框架-->设置-----------------------
    public static float ManualWidth = 1200;// 用于各种messagebox文字换行的处理
    public static float ManualHeight = 1920.0f;// 用于各种messagebox文字换行的处理

    public static string mMessageBoxWndTitle;//消息标题
    public static readonly string UILoadingTweenPath = "LocalRes/SwitchSceneTween"; // 过渡动画的地址，放在Resources里

    #endregion

    private static void InitializeUrls()
    {
        var settings = Resources.Load<EnvironmentSettings>("EnvironmentSettings");
        if (settings == null)
        {
            Debug.LogError("EnvironmentSettings asset not found in Resources folder.");
            return;
        }

        switch (settings.currentEnvironment)
        {
            case YooAssetEnvironmentType.Loacal:
                BundleServerURL = $"{localIp}/{BundleVer}";
                ServerURL = "http://localhost/api";
                break;
            case YooAssetEnvironmentType.DevEnv:
                BundleServerURL = "http://devserver/bundles";
                ServerURL = "http://devserver/api";
                break;
            case YooAssetEnvironmentType.ProductionEnv:
                BundleServerURL = "http://prodserver/bundles";
                ServerURL = "http://prodserver/api";
                break;
            default:
                BundleServerURL = "http://localhost/bundles";
                ServerURL = "http://localhost/api";
                break;
        }
    }

    public static string GetBundleURL()
    {
        return BundleServerURL;
    }

    public static string GetServerURL()
    {
        return ServerURL;
    }


    
    public static string mMessageBoxWndContent;//消息内容
    public static MessageBoxWnd.Style mMessageBoxWndStyle = MessageBoxWnd.Style.OK_CANCLE;//消息模式
    public static MessageBoxWnd.StyleExt mMessageBoxWndStyleExt = MessageBoxWnd.StyleExt.Solid;//离开模式
    public static object[] mMessageBoxWndArgs = null;//消息的可变参数
    
    public static string RewardName { get; set; }
    public static string ADUserID { set; get; }
    public static int RewardAmount { set; get; }

    public static DateTime mSystemTime;
 
    //玩家当前账号的ID
    static int _id;
    public static int id { get { return _id; } set { _id = value; } }


    static int _enterScene;
    public static int enterScene { set { _enterScene = value; } get { return _enterScene; } }//1、主场景 2、大地图

    public static void MessageBoxWndShow(string title, string content, MessageBoxWnd.StyleExt mStyleExt)
    {
        mMessageBoxWndStyleExt = mStyleExt;//为窗口的关闭类型赋值

        mMessageBoxWndTitle = title;//标题
        mMessageBoxWndContent = content;
    }

    public static void MessageBoxWndShow(string title, string content, MessageBoxWnd.Style styl)
    {
        AudioController.Instance.SoundPlay(AudioPlayerName.Pop, 0.8f);
        mMessageBoxWndStyle = styl;//为窗口的类型赋值

        mMessageBoxWndTitle = title;//标题
        mMessageBoxWndContent = content;
    }

    public static void MessageBoxWndShow(string title, string content, MessageBoxWnd.Style styl, MessageBoxWnd.StyleExt mStyleExt, params object[] Args)
    {
        mMessageBoxWndStyle = styl;//为窗口的类型赋值
        mMessageBoxWndStyleExt = mStyleExt;//为窗口的关闭类型赋值

        mMessageBoxWndTitle = title;//标题
        mMessageBoxWndContent = content;

        mMessageBoxWndArgs = Args;
    }


    public static void SetRectTransform()
    {
        GameObject mCanvas = GameObject.Find("Canvas");

        for (int i = 0; i < mCanvas.transform.childCount; i++)
        {
            GameObject go = mCanvas.transform.GetChild(i).gameObject;

            go.gameObject.GetComponent<RectTransform>().localScale = new Vector3(1.5f, 1.5f, 1.5f);
        }
    }

    /// <summary>
    /// 删除所有子物体
    /// </summary>
    /// <param name="mGameObject"></param>
    public static void DestroyChild(GameObject mGameObject)
    {
        for (int i = 0; i < mGameObject.transform.childCount; i++)
        {
            GameObject go = mGameObject.transform.GetChild(i).gameObject;
            GameObject.Destroy(go);
        }
    }

    public static string getNumText(int num)
    {
        string numText = num.ToString();
        int i = 0;
        while (true)
        {
            if (num / 10000 >= 1)
            {
                i++;
                num = num / 10000;
            }
            else
            {
                break;
            }
        }
        switch (i)
        {
            case 0:
                numText = num.ToString();
                break;
            case 1:
                numText = num + "万";
                break;
            case 2:
                numText = num + "亿";
                break;
            case 3:
                numText = num + "兆";
                break;
            case 4:
                numText = num + "京";
                break;
            case 5:
                numText = num + "垓";
                break;
        }

        return numText;
    }

    public static string getMessage(string message)
    {
        if (message == null || message == string.Empty)
        {
            return message;
        }
        //貌似是屏蔽字库判断
        //if (ShieldFontEntityList != null && ShieldFontEntityList.Count > 0)
        //{
        //    foreach (ShieldFontEntity t in ShieldFontEntityList)
        //    {
        //        if (message.Contains(t.Message))
        //        {
        //            string shield = "";
        //            for (int i = 0; i < t.Message.Length; i++)
        //            {
        //                shield += "*";
        //            }
        //            message = message.Replace(t.Message, shield);
        //        }
        //    }
        //}
        //return message;
        return null;
    }


}


